/*
 * TowerGreenTwo.java
 *
 * Created on December 16, 2007, 7:22 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package hextd.tower;

import hextd.hex.Hex;
import hextd.sprite.EnemySprite;
import hextd.util.Context;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;

/**
 *
 * @author hylke
 */
public class TowerGreenTwo extends TowerBase {
    
    public static int price = 7000;
    public static int damage = 500;
    public static float range = 4.5f;
    
    private int[] fireAt = {-1, -1};
    private float[] enemyX = {0, 0};
    private float[] enemyY = {0, 0};
    private EnemySprite[] enemy = new EnemySprite[2];
    private float rJump = 2.5f;
    private EnemySprite.COLOR damageColor = EnemySprite.COLOR.GREEN;
    private Color lineColorDark;
    
    /** Creates a new instance of TowerGreenTwo */
    public TowerGreenTwo(Context context, Hex hex) {
        super(TowerFactory.type.greenTwo, price, damage, range);
        this.lineColor = Color.GREEN;
        this.lineColorDark = new Color(this.lineColor.getRed()/2, this.lineColor.getGreen()/2, this.lineColor.getBlue()/2);
        this.doInit(context, hex);
    }
    
    public void doTick(int gameTime) {
        this.fireAt[1] = -1;
        this.fireAt[0] = this.findEnemy(this.fireAt[0], false);
        if (this.fireAt[0] >= 0) {
            this.enemy[0] = this.context.enemies[this.fireAt[0]];
            this.enemyX[0] = this.enemy[0].getX();
            this.enemyY[0] = this.enemy[0].getY();
            this.enemy[0].doDamage(this.damageCurrent, this.damageColor);
            this.fireAt[1] = this.findEnemyClosest(this.enemyX[0], this.enemyY[0], this.rJump, this.fireAt);
            if (this.fireAt[1] >= 0) {
                this.enemy[1] = this.context.enemies[this.fireAt[1]];
                this.enemyX[1] = this.enemy[1].getX();
                this.enemyY[1] = this.enemy[1].getY();
                this.enemy[1].doDamage(this.damageCurrent, this.damageColor);
            }
        }
    }
    
    public String getStatusString() {
        String retString = "";
        retString += "<h1>Green Tower 2</h1>" +
                "<p>" +
                "<b>Level:</b> "+(1+this.level)+"<br>\n" +
                "<b>Damage:</b> "+this.damageText+"<br>\n" +
                "<b>Range:</b> "+this.rangeText+"<br>" +
                "</p>\n";
        return retString;
    }
    
    public String getInfoString() {
        String retString = "";
        retString += "<h1>Green Tower 2</h1>" +
                "<p>" +
                "Tower with one medium laser that refracts from the first target to a second. Both targets receive full damage.<br>" +
                "Strong against green enemies, very weak against red enemies.</p>" +
                "<p>" +
                "<b>Price:</b> "+this.form.format(this.price)+"<br>" +
                "<b>Damage:</b> "+this.form.format(this.damageCurrent)+"<br>" +
                "<b>Range:</b> "+this.form.format(rangeCurrent)+"<br>" +
                "<b>Refire rate:</b> 20/s"+
                "</p>";
        return retString;
    }
    
    protected void calcDamageRange() {
        super.calcDamageRange();
    }
    
    protected void generateShapes() {
        float width = this.context.hexPaintWidth;
        int shapeCount = 6;
        this.shapes = new Shape[shapeCount];
        this.shapes[0] = new Ellipse2D.Float(-0.5f*this.paintScale, -0.5f*this.paintScale, 1.0f*this.paintScale, 1.0f*this.paintScale);
        this.shapes[1] = new Ellipse2D.Float(-0.25f*this.paintScale, -0.25f*this.paintScale, 0.5f*this.paintScale, 0.5f*this.paintScale);
        this.shapes[2] = new Line2D.Float(0, 0.5f*paintScale, 0, paintScale);
        this.shapes[3] = new Line2D.Float(-0.5f*width, -0.5f*0.5f*paintScale, -width, -0.5f*paintScale);
        this.shapes[4] = new Line2D.Float( 0.5f*width, -0.5f*0.5f*paintScale,  width, -0.5f*paintScale);
        this.shapes[5] = new Ellipse2D.Float(-1.0f*this.context.strokeScale, -1.0f*this.context.strokeScale, 2.0f*this.context.strokeScale, 2.0f*this.context.strokeScale);
        
        this.shapeAction = new int[shapeCount];
        this.shapeAction[0] = 1;
        this.shapeAction[1] = 1;
        this.shapeAction[2] = 1;
        this.shapeAction[3] = 1;
        this.shapeAction[4] = 1;
        this.shapeAction[5] = 1;
        
        this.shapeColor = new Color[shapeCount];
        this.shapeColor[0] = this.lineColor;
        this.shapeColor[1] = this.lineColorDark;
        this.shapeColor[2] = this.lineColor;
        this.shapeColor[3] = this.lineColor;
        this.shapeColor[4] = this.lineColor;
        this.shapeColor[5] = this.lineColor;
        
        this.strokes = new Stroke[shapeCount];
        this.strokes[0] = new BasicStroke(2.0f*this.context.strokeScale, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL);
        this.strokes[1] = this.strokes[0];
        this.strokes[2] = this.strokes[0];
        this.strokes[3] = this.strokes[0];
        this.strokes[4] = this.strokes[0];
        this.strokes[5] = this.strokes[0];
    }
    
    public void paintEffects(Graphics2D g2, int gameTime) {
        Stroke defaultStroke = g2.getStroke();
        
        if (this.fireAt[0] > -1) {
            g2.setColor(this.lineColor);
            float e1x = this.enemy[0].getPaintX() + ( (float)(Math.random()-0.5)*this.paintScale/10f );
            float e1y = this.enemy[0].getPaintY() + ( (float)(Math.random()-0.5)*this.paintScale/10f );
            g2.draw(new Line2D.Float(this.xPaint, this.yPaint, e1x, e1y));
            if (this.fireAt[1] > -1) {
                float e2x = this.enemy[1].getPaintX() + ( (float)(Math.random()-0.5)*this.paintScale/10f );
                float e2y = this.enemy[1].getPaintY() + ( (float)(Math.random()-0.5)*this.paintScale/10f );
                g2.draw(new Line2D.Float(e2x, e2y, e1x, e1y));
            }
        }
        
        g2.setStroke(defaultStroke);
    }
}
